package com.sharp.oss.service;

import com.github.pagehelper.PageInfo;
import com.sharp.kernel.constant.db.SqlConstant;
import com.sharp.framework.service.cache.FrameCacheService;
import com.sharp.kernel.utils.BeanUtil;
import com.sharp.kernel.utils.DbUtil;
import com.sharp.kernel.utils.ReflectionUtil;
import com.sharp.kernel.utils.StringUtil;
import com.sharp.framework.utils.security.MD5Util;
import com.sharp.kernel.vo.query.QueryRespVO;
import com.sharp.oss.constant.ObjectStorageConst;
import com.sharp.oss.constant.param.ObjectStorageParamConst;
import com.sharp.oss.dao.entity.SaObjectStorage;
import com.sharp.oss.dao.repository.SaObjectStorageService;
import com.sharp.oss.vo.UploadVO;
import com.sharp.oss.vo.oss.GetOssFileInfo;
import com.sharp.oss.vo.oss.QueryOssReq;
import com.sharp.oss.vo.oss.QueryOssRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Title: ObjectStorageService
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2019/10/16 21:09
 * @description Project Name:
 * @Package: com.srct.service.service
 */
public interface ObjectStorageService {

    /**
     * 获取上传那文件详细信息
     *
     * @param req
     * @return
     */
    default QueryOssRes get(GetOssFileInfo req) {
        SaObjectStorageService repository = BeanUtil.getBean(SaObjectStorageService.class);
        SaObjectStorage saObjectStorage = repository.getSaObjectStorageByFileName(req.getFileName());
        QueryOssRes res = ReflectionUtil.copy(saObjectStorage, QueryOssRes.class);
        return res;
    }

    /**
     * 上传文件并存储
     *
     * @param request  http请求
     * @param file     文件
     * @param fileName 文件名
     * @param type     文件类型
     * @return 返回类型
     */
    UploadVO upload(HttpServletRequest request, MultipartFile file, String fileName, String type);

    /**
     * 生成文件名
     *
     * @param originalName 原文件名
     * @param ip           访问ip
     * @return 生成文件名
     */
    default String buildFileName(String originalName, String ip) {
        String suffix = "";
        if (StringUtils.isNotBlank(originalName)) {
            suffix = originalName.substring(originalName.lastIndexOf("."));
        }
        return MD5Util.MD5(originalName + ip + (new Date()).toString()) + suffix;
    }

    /**
     * 构建返回类型
     *
     * @param fileName 文件名
     * @param fileSize 文件大小
     * @return 返回类型
     */
    default UploadVO buildUploadVO(String fileName, Long fileSize, String url) {
        UploadVO res = new UploadVO();
        res.setFileName(fileName);
        res.setFileSize(fileSize);
        res.setUrl(url);
        return res;
    }

    /**
     * 上传记录
     *
     * @param type     存储类型
     * @param uri      访问地址
     * @param fileSize 大小
     */

    default void record(String name, String type, String uri, Long fileSize) {
        SaObjectStorageService repository = BeanUtil.getBean(SaObjectStorageService.class);
        SaObjectStorage os = new SaObjectStorage();
        os.setName(name);
        os.setUri(uri + "?t=" + System.currentTimeMillis());
        os.setType(type);
        os.setSize(fileSize);
        os.setValid(SqlConstant.VALID);
        repository.insertOrUpdateSelective(os, "uri");
    }

    /**
     * 查询上传数据
     *
     * @param req
     * @return
     */
    default QueryRespVO<QueryOssRes> query(QueryOssReq req) {
        SaObjectStorageService repository = BeanUtil.getBean(SaObjectStorageService.class);
        Example example = repository.getExample();
        Example.Criteria criteria = example.createCriteria();
        String name = StringUtil.parse(req.getQueryCondition().get("name"));
        String type = StringUtil.parse(req.getQueryCondition().get("type"));
        if (StringUtils.isNotBlank(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(type)) {
            criteria.andEqualTo("type", type);
        }
        example.orderBy("updateAt").desc();
        PageInfo<SaObjectStorage> pageInfo = DbUtil.buildPageInfo(req, SaObjectStorage.class);
        PageInfo<SaObjectStorage> data = repository.select(example, pageInfo);
        return new QueryRespVO<>(QueryOssRes.class, data);
    }

    default String buildAccessUrl(String folderName, String fileName) {
        FrameCacheService frameCacheService = BeanUtil.getBean(FrameCacheService.class);
        String domain = frameCacheService.getParamValueAvail(ObjectStorageParamConst.OSS_ACCESS_DOMAIN);
        if (!StringUtils.endsWith(folderName, ObjectStorageConst.SEP)) {
            folderName = folderName + ObjectStorageConst.SEP;
        }
        return domain + folderName + fileName;
    }

    /**
     * 删除上传数据
     *
     * @param id
     */
    void delete(Long id);


}
